import { MessageBox, Loading } from 'element-ui'
import {store} from "core-js/internals/reflect-metadata";

let loading
function startLoading({ url }) {
  // 使用Element loading-start 方法
  loading = Loading.service({
    lock: true,
    text: '加载中……',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
}

function endLoading() {
  // 使用Element loading-close 方法
  loading && loading.close()
}

export default ({$axios, store}) => {
  //Request拦截器，设置token
  $axios.onRequest((config)=>{
    if(store.state.user.token){
      config.headers['Authorization'] = store.state.token
      startLoading(config)
    }
  })

   // 错误回调
  $axios.onError((error) => {
    endLoading()
  })

  $axios.onResponse((response) => {
    // return response.data
    endLoading()
  })
}
